﻿package com.controller;

import java.io.File;
import java.io.IOException;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.entity.Chengshixinxi;
import com.server.ChengshixinxiServer;
import com.util.PageBean;
import net.sf.json.JSONObject;
import com.util.db;
import java.sql.SQLException;
import java.sql.*;
@Controller
public class ChengshixinxiController {
	@Resource
	private ChengshixinxiServer chengshixinxiService;


   
	@RequestMapping("addChengshixinxi.do")
	public String addChengshixinxi(HttpServletRequest request,Chengshixinxi chengshixinxi,HttpSession session) throws SQLException{
		Timestamp time=new Timestamp(System.currentTimeMillis());
		
		chengshixinxi.setAddtime(time.toString().substring(0, 19));
		chengshixinxiService.add(chengshixinxi);
		session.setAttribute("backxx", "添加成功");
		session.setAttribute("backurl", request.getHeader("Referer"));
		
		//session.setAttribute("backurl", "chengshixinxiList.do");
		
		return "redirect:postback.jsp";
		//return "redirect:chengshixinxiList.do";
		
		
		
	}
 
//	处理编辑
	@RequestMapping("doUpdateChengshixinxi.do")
	public String doUpdateChengshixinxi(int id,ModelMap map,Chengshixinxi chengshixinxi){
		chengshixinxi=chengshixinxiService.getById(id);
		map.put("chengshixinxi", chengshixinxi);
		return "chengshixinxi_updt";
	}
	
	
	
	
	
//	后台详细
	@RequestMapping("chengshixinxiDetail.do")
	public String chengshixinxiDetail(int id,ModelMap map,Chengshixinxi chengshixinxi){
		chengshixinxi=chengshixinxiService.getById(id);
		map.put("chengshixinxi", chengshixinxi);
		return "chengshixinxi_detail";
	}
//	前台详细
	@RequestMapping("csxxDetail.do")
	public String csxxDetail(int id,ModelMap map,Chengshixinxi chengshixinxi){
		chengshixinxi=chengshixinxiService.getById(id);
		map.put("chengshixinxi", chengshixinxi);
		return "chengshixinxidetail";
	}
//	
	@RequestMapping("updateChengshixinxi.do")
	public String updateChengshixinxi(int id,ModelMap map,Chengshixinxi chengshixinxi,HttpServletRequest request,HttpSession session){
		chengshixinxiService.update(chengshixinxi);
		session.setAttribute("backxx", "修改成功");
		session.setAttribute("backurl", request.getHeader("Referer"));
		return "redirect:postback.jsp";
		//String url = request.getHeader("Referer");
		//return "redirect:"+url;
		//return "redirect:chengshixinxiList.do";
	}

//	分页查询
	@RequestMapping("chengshixinxiList.do")
	public String chengshixinxiList(@RequestParam(value="page",required=false)String page,
			ModelMap map,HttpSession session,Chengshixinxi chengshixinxi, String chengshi){
		if(page==null||page.equals("")){
			page="1";
		}
		PageBean pageBean=new PageBean(Integer.parseInt(page), 10);
		Map<String, Object> pmap=new HashMap<String,Object>();
		pmap.put("pageno", pageBean.getStart());
		pmap.put("pageSize", 10);
		
		
		if(chengshi==null||chengshi.equals("")){pmap.put("chengshi", null);}else{pmap.put("chengshi", chengshi);}		
		int total=chengshixinxiService.getCount(pmap);
		pageBean.setTotal(total);
		List<Chengshixinxi> list=chengshixinxiService.getByPage(pmap);
		map.put("page", pageBean);
		map.put("list", list);
		session.setAttribute("p", 1);
		return "chengshixinxi_list";
	}
	
	
	
	@RequestMapping("csxxList.do")
	public String csxxList(@RequestParam(value="page",required=false)String page,
			ModelMap map,HttpSession session,Chengshixinxi chengshixinxi, String chengshi){
		if(page==null||page.equals("")){
			page="1";
		}
		PageBean pageBean=new PageBean(Integer.parseInt(page), 10);
		Map<String, Object> pmap=new HashMap<String,Object>();
		pmap.put("pageno", pageBean.getStart());
		pmap.put("pageSize", 10);
		
		
		if(chengshi==null||chengshi.equals("")){pmap.put("chengshi", null);}else{pmap.put("chengshi", chengshi);}		
		int total=chengshixinxiService.getCount(pmap);
		pageBean.setTotal(total);
		List<Chengshixinxi> list=chengshixinxiService.getByPage(pmap);
		map.put("page", pageBean);
		map.put("list", list);
		session.setAttribute("p", 1);
		return "chengshixinxilist";
	}
	
	@RequestMapping("deleteChengshixinxi.do")
	public String deleteChengshixinxi(int id,HttpServletRequest request){
		chengshixinxiService.delete(id);
		String url = request.getHeader("Referer");
		return "redirect:"+url;
		//return "redirect:chengshixinxiList.do";
	}
	
	
}
